﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Admin_Admin_DocumentCategory : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        InitMenu();
        if (!IsPostBack)
        {
            OfficeDataClassesDataContext dc = new OfficeDataClassesDataContext();
            var document_categorys = from document_category in dc.Document_Categories
                                     select new
                                     {
                                         标识 = document_category.Category_id,
                                         标题 = document_category.Category_name
                                     };
            DocumentCategoryGridView.DataSource = document_categorys;
            DocumentCategoryGridView.DataBind();
            dc.Dispose();
        }
    }

    protected void InitMenu()
    {

        OfficeDataClassesDataContext dc = new OfficeDataClassesDataContext();
        Menu1.Items.Clear();
        MenuItem MainPage = new MenuItem();
        MainPage.Enabled = true;
        MainPage.Selectable = true;
        MainPage.Selected = false;
        MainPage.Value = MainPage.ToolTip = MainPage.Text = "注销登陆";
        MainPage.NavigateUrl = "../Default.aspx";
        Menu1.Items.Add(MainPage);

        MenuItem navigation = new MenuItem();
        navigation.Value = navigation.ToolTip = navigation.Text = "页眉管理";
        navigation.NavigateUrl = "Admin_navigation.aspx";
        Menu1.Items.Add(navigation);

        MenuItem news = new MenuItem();
        news.Value = news.ToolTip = news.Text = "重要通知";
        Menu1.Items.Add(news);

        MenuItem catergory1 = new MenuItem();//定义子菜单
        catergory1.Text = "类别管理";
        catergory1.NavigateUrl = "Admin_DocumentCategory.aspx";
        Menu1.Items[2].ChildItems.Add(catergory1);//添加子菜单

        MenuItem aa1 = new MenuItem();
        aa1.Text = "重要通知管理";
        aa1.NavigateUrl = "Admin_Notice.aspx";
        Menu1.Items[2].ChildItems.Add(aa1);


        MenuItem document = new MenuItem();
        document.Value = document.ToolTip = document.Text = "校发文件";
        Menu1.Items.Add(document);

        MenuItem catergory2 = new MenuItem();//定义子菜单
        catergory2.Text = "类别管理";
        catergory2.NavigateUrl = "Admin_DocumentCategory.aspx";
        Menu1.Items[3].ChildItems.Add(catergory2);//添加子菜单

        MenuItem aa2 = new MenuItem();
        aa2.Text = "校发文件管理";
        aa2.NavigateUrl = "Admin_Document.aspx";
        Menu1.Items[3].ChildItems.Add(aa2);





        int a = 0;

        var Menus = from menu in dc.Menus
                    select new
                    {
                        MenuItem_id = menu.MenuItem_id,
                        MenuItem_Name = menu.MenuItem_Name
                    };

        foreach (var menu in Menus)
        {
            MenuItem menuitem = new MenuItem();
            menuitem.Enabled = true;
            menuitem.Selectable = true;
            menuitem.Selected = false;
            menuitem.ToolTip = menuitem.Text = menu.MenuItem_Name.Trim();
            menuitem.Value = menu.MenuItem_id.ToString();
            Menu1.Items.Add(menuitem);
            a++;
            MenuItem catergory = new MenuItem();//定义子菜单
            catergory.Text = "类别管理";
            catergory.NavigateUrl = "Admin_DocumentCategory.aspx";
            Menu1.Items[a + 3].ChildItems.Add(catergory);//添加子菜单

            MenuItem aa = new MenuItem();

            aa.Text = menu.MenuItem_Name.ToString();


            aa.Text = aa.Text.Trim() + "管理";
            Menu1.Items[a + 3].ChildItems.Add(aa);

        }
        Menu1.Items[4].ChildItems[0].NavigateUrl = "";
        Menu1.Items[4].ChildItems[1].NavigateUrl = "";
        Menu1.Items[5].ChildItems[1].NavigateUrl = "Admin_Orgnization.aspx";
        Menu1.Items[6].ChildItems[1].NavigateUrl = "Admin_Rule.aspx";
        Menu1.Items[7].ChildItems[1].NavigateUrl = "Admin_Meeting.aspx";
        Menu1.Items[8].ChildItems[1].NavigateUrl = "Admin_CollegeCondition.aspx";
        Menu1.Items[9].ChildItems[1].NavigateUrl = "Admin_Law.aspx";
        Menu1.Items[10].ChildItems[1].NavigateUrl = "Admin_Download.aspx";







        MenuItem link = new MenuItem();
        link.Value = link.ToolTip = link.Text = "链接管理";
        link.NavigateUrl = "Admin_Link.aspx";
        Menu1.Items.Add(link);

        MenuItem password = new MenuItem();
        password.Value = password.ToolTip = password.Text = "密码管理";
        password.NavigateUrl = "Admin_Password.aspx";
        Menu1.Items.Add(password);


        dc.Dispose();
    }
    protected void Logoff_Click(object sender, EventArgs e)
    {
        Session.Remove("admin");
        Response.Redirect("../Default.aspx");
    }
    protected void NoticeManagement_Click(object sender, EventArgs e)
    {
        Response.Redirect("Admin_Notice.aspx");
    }
    protected void DocumentCategoryManagement_Click(object sender, EventArgs e)
    {
        Response.Redirect("Admin_DocumentCategory.aspx");
    }
    protected void DocumentManagement_Click(object sender, EventArgs e)
    {
        Response.Redirect("Admin_Document.aspx");
    }
    protected void DownloadManagement_Click(object sender, EventArgs e)
    {
        Response.Redirect("Admin_Download.aspx");
    }
    protected void PasswordManagement_Click(object sender, EventArgs e)
    {
        Response.Redirect("Admin_Password.aspx");
    }
    protected void CollegeConditionManagement_Click(object sender, EventArgs e)
    {
        Response.Redirect("Admin_CollegeCondition.aspx");
    }
    protected void LawManagement_Click(object sender, EventArgs e)
    {
        Response.Redirect("Admin_Law.aspx");
    }
    protected void MeetingManagement_Click(object sender, EventArgs e)
    {
        Response.Redirect("Admin_Meeting.aspx");
    }
    protected void OrgnizationManagement_Click(object sender, EventArgs e)
    {
        Response.Redirect("Admin_Orgnization.aspx");
    }
    protected void RuleManagement_Click(object sender, EventArgs e)
    {
        Response.Redirect("Admin_Rule.aspx");
    }
    protected void LinkManagement_Click(object sender, EventArgs e)
    {
        Response.Redirect("Admin_Link.aspx");
    }
    protected void NewCategory_Click(object sender, EventArgs e)
    {

        if (New_Category.Text.Trim().Length == 0)
        {
            JavaScript.JSDialog.Alert(this, "无法添加名称为空的类别!");
        }
        else 
        {
            Document_Category document_cate = new Document_Category();
            document_cate.Category_name = New_Category.Text.Trim();
            try
            {
                SQL.SQLHelper.Insert(document_cate);
            }
            catch (Exception ex)
            { 
            }
            New_Category.Text = "";
            OfficeDataClassesDataContext dc = new OfficeDataClassesDataContext();
            var document_categorys = from document_category in dc.Document_Categories
                                     select new
                                     {
                                         标识 = document_category.Category_id,
                                         标题 = document_category.Category_name
                                     };
            DocumentCategoryGridView.DataSource = document_categorys;
            DocumentCategoryGridView.DataBind();
            dc.Dispose();
        }
    }
    protected void MotifyCategory_Click(object sender, EventArgs e)
    {
        Motify_Category.Visible = false;
        MotifyCategory.Visible = false;

        OfficeDataClassesDataContext dc = new OfficeDataClassesDataContext();
        var document_category = from document_cate in dc.Document_Categories
                                where document_cate.Category_id.Equals(Convert.ToInt32(Motify_CategoryID.Text.Trim()))
                                select document_cate;
        if(!document_category.ToArray().ElementAt(0).Category_name.Equals(Motify_Category.Text.Trim()))
        {
            document_category.ToArray().ElementAt(0).Category_name = Motify_Category.Text.Trim();
            dc.SubmitChanges();
        }

        var document_categorys = from document_cate in dc.Document_Categories
                                 select new
                                 {
                                     标识 = document_cate.Category_id,
                                     标题 = document_cate.Category_name
                                 };
        DocumentCategoryGridView.DataSource = document_categorys;
        DocumentCategoryGridView.DataBind();
        dc.Dispose();
    }
    protected void DocumentCategoryGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        OfficeDataClassesDataContext dc = new OfficeDataClassesDataContext();

        var delete_document = from document in dc.Documents
                              where document.Document_category.Equals(Convert.ToInt32(((Label)DocumentCategoryGridView.Rows[e.RowIndex].FindControl("News_id")).Text))
                              select document;

        try 
        {
            dc.Documents.DeleteAllOnSubmit(delete_document);
            dc.SubmitChanges();
        }
        catch (Exception ex)
        {
        }
        
        var delete_document_cates = from document_category in dc.Document_Categories
                                    where document_category.Category_id.Equals(Convert.ToInt32(((Label)DocumentCategoryGridView.Rows[e.RowIndex].FindControl("News_id")).Text))
                                    select document_category;
        try
        {
            dc.Document_Categories.DeleteAllOnSubmit(delete_document_cates);
            dc.SubmitChanges();
        }
        catch (Exception ex)
        {
        }
        var document_categorys = from document_category in dc.Document_Categories
                                 select new
                                 {
                                     标识 = document_category.Category_id,
                                     标题 = document_category.Category_name
                                 };
        DocumentCategoryGridView.DataSource = document_categorys;
        DocumentCategoryGridView.DataBind();
        dc.Dispose();

    }
    protected void DocumentCategoryGridView_RowEditing(object sender, GridViewEditEventArgs e)
    {
        Motify_Category.Visible = true;
        MotifyCategory.Visible = true;
        Modify_Category_Cancle.Visible = true;
        Motify_CategoryID.Text = ((Label)DocumentCategoryGridView.Rows[e.NewEditIndex].FindControl("News_id")).Text.Trim();
        Motify_Category.Text = ((Label)DocumentCategoryGridView.Rows[e.NewEditIndex].FindControl("News_Title")).Text.Trim();
    }

    protected void New_Click(object sender, EventArgs e)
    {
        New_Category_Name.Visible = true;
        New_Category.Visible = true;
        NewCategory.Visible = true;
        New_Category_Cancle.Visible = true;
    }
    protected void Modify_Category_Cancle_Click(object sender, EventArgs e)
    {
        Motify_Category.Visible = false;
        MotifyCategory.Visible = false;
        Modify_Category_Cancle.Visible = false;
    }
    protected void New_Category_Cancle_Click(object sender, EventArgs e)
    {
        New_Category_Name.Visible = false;
        New_Category.Visible = false;
        NewCategory.Visible = false;
        New_Category_Cancle.Visible = false;
    }
}
